package pl.plesken.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;

public class GenreEdit extends Dialog {

	private Text text;
	protected Object result;
	protected Shell shell;
	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public GenreEdit(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public GenreEdit(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(236, 155);
		shell.setText(SetLanguage.getString("GenreEdit.AppInfo")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout());

		final Group nameGroup = new Group(composite, SWT.NONE);
		nameGroup.setText(SetLanguage.getString("GenreEdit.changeName")); //$NON-NLS-1$
		final GridData gd_nameGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		nameGroup.setLayoutData(gd_nameGroup);
		nameGroup.setLayout(new GridLayout());

		text = new Text(nameGroup, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		text.setText(name);

		final Group group = new Group(composite, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);

		final Button setButton = new Button(group, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				doIt();
			}
		});
		final GridData gd_setButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_setButton.widthHint = 70;
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("GenreEdit.ok")); //$NON-NLS-1$

		final Button cancelButton = new Button(group, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 70;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("GenreEdit.cancel")); //$NON-NLS-1$
		//
	}

	private void doIt() {
		name = text.getText();
		shell.close();
	}
	
}
